<?php

namespace app\admin\controller;

use think\Request;
use think\facade\Session;
use think\facade\Config;
use app\admin\model\AuthRule as AuthModel;
use app\admin\model\Member as MemberModel;
use app\common\controller\Base as CommonBase;

class Login extends CommonBase
{
    function index(){
        $this->redirect("login");
    }

    function login(){
        $member = login_member();
        if($member){
            if($this->request->isGet()){
                $this->redirect("index/index");
            }else{
                $this->success("登录成功", "index/index", '', 0);
            }
        }

        if($this->request->isPost()){
            $param = $this->request->param();
            $Model = new MemberModel();
            $login_res = $Model->doLogin($param["name"], $param["password"]);
            if(!$login_res['status']){
                $this->error($login_res['msg']);
            }else{
                if(isset($param["is_remember"])){
                    $expire = 7*24*3600;
                    Session::setConfig(["expire" => $expire]);
                }
                Session::set("member", $login_res['data']);

                $back_url = Session::pull('back_url');
                if(empty($back_url)){
                    $back_url = url("index/index");
                }
                $this->success("登录成功", $back_url);
            }
        }else{
            return $this->fetch("login");
        }
    }

    function logout(){
        $AuthModel = new AuthModel;
        $AuthModel->clearMemberSession();
        Session::delete("member");
        $this->redirect("login");
    }
}
